﻿Imports System.Data.OleDb
Partial Public Class UsersProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack() Then
            Dim DoesUserExistcmd = "SELECT * FROM Designer WHERE UserName ='" & User.Identity.Name & "'"
            Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("mobilestandardsdb").ConnectionString)
            oleDbConn.Open()
            Dim dbcmd As OleDbCommand = New OleDbCommand(DoesUserExistcmd, oleDbConn)
            Dim dbread = dbcmd.ExecuteReader()
            If Not dbread.HasRows() Then
                Dim newUserCmd = "INSERT INTO Designer (UserName) VALUES('" & User.Identity.Name & "')"
                Dim newUserCmdobj As OleDbCommand = New OleDbCommand(newUserCmd, oleDbConn)
                newUserCmdobj.ExecuteNonQuery()
            End If
        Else
            Dim oleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("mobilestandardsdb").ConnectionString)
            oleDbConn.Open()
            Dim sqlstring As String = "UPDATE Designer SET FirstName=@FirstName,LastName=@LastName,Gender=@Gender,Company=@Company,Location=@Location,ProfileImage=@ProfileImage WHERE UserName = @UserName"
            Dim dbcmdObj = New OleDbCommand(sqlstring, oleDbConn)
            Dim fileloc = Request.MapPath("UserImages") & "\" & User.Identity.Name & ".jpg"
            Me.ProfileImage.SaveAs(fileloc)
            dbcmdObj.Parameters.AddWithValue("@FirstName", FirstName.Text)
            dbcmdObj.Parameters.AddWithValue("@LastName", LastName.Text)
            dbcmdObj.Parameters.AddWithValue("@Gender", Gender.SelectedValue)
            dbcmdObj.Parameters.AddWithValue("@Company", Company.Text)
            dbcmdObj.Parameters.AddWithValue("@Location", Location.Text)
            dbcmdObj.Parameters.AddWithValue("@ProfileImage", fileloc)
            dbcmdObj.Parameters.AddWithValue("@UserName", User.Identity.Name)
            dbcmdObj.ExecuteNonQuery()
        End If
    End Sub
End Class